package behavioral.chain_of_responsibility.impl1;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: HandlerChain
 * @Description: 客户类（Client）角色：创建处理链，并向链头的具体处理者对象提交请求，它不关心处理细节和请求的传递过程
 * @Author: liu-hao
 * @Date: 2023-04-09 16:48
 * @Version: 1.0
 **/
public class HandlerChain {
    private final List<AbstractHandler> handlerList = new ArrayList<>();

    public HandlerChain addHandler(AbstractHandler handler) {
        handlerList.add(handler);
        return this;
    }

    public boolean handle() {
        boolean resultFlag = true;
        for (AbstractHandler handler : handlerList) {
            if (!handler.handler()) {
                resultFlag = false;
                break;
            }
        }
        return resultFlag;
    }
}
